package com.servlet;

import com.dao.BooksDao;
import com.dao.impl.BooksDaoImpl;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.util.JsonUtil;
import com.util.ProxyUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

@WebServlet("/DelectServlet")

public class DelectServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        BooksDao booksDao=(BooksDao) ProxyUtil.getDaoProxy(new BooksDaoImpl());
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        JsonUtil jsonUtil =new JsonUtil();
         String id = request.getParameter("id");
         int i = Integer.parseInt(id);
        System.out.println(i);

        try {
            int i1 = booksDao.deleteById(i);
            if (i1>0){
                jsonUtil.setCode(0);
                jsonUtil.setMsg("删除成功");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            jsonUtil.setCode(1);
            jsonUtil.setMsg("系统错误");
        }

        GsonBuilder gsonBuilder = new GsonBuilder();
        Gson gson = gsonBuilder.create();
        String jsonstr = gson.toJson(jsonUtil);

        System.out.println(jsonstr);

        out.write(jsonstr);

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

}
 
 
 

